using System.Collections;
using System.Collections.Generic;
using MyFrame.Mono;
using MyFrame.Single;
using UnityEngine;


namespace MyFrame.Load
{
    public class ResMgr : Singleton<ResMgr>
    {
        public T Load<T>(string name) where T : Object
        {
            T res = Resources.Load<T>(name);

            if (res is GameObject)
            {
                return GameObject.Instantiate(res) as T;
            }

            return res;
        }

        //异步加载
        public void LoadAsync<T>(string name, System.Action<T> callback) where T : Object
        {
             MonoMgr.Instance.StartCoroutine(LoadAsyncCoroutine(name, callback));
        }

        private IEnumerator LoadAsyncCoroutine<T>(string name, System.Action<T> callback) where T : Object
        {
            ResourceRequest request = Resources.LoadAsync<T>(name);
            yield return request;

            if (request.asset is GameObject)
            {
                callback(GameObject.Instantiate(request.asset) as T);
            }
            else
            {
                callback(request.asset as T);
            }
        }
    }

}
